package com.itheima.jsoup;

import com.itheima.httpClient.HttpClientGet;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.net.URL;
import java.net.http.HttpClient;

public class JsoupDoc {


    //使用jsoup获取document的方式
    public static void main(String[] args) throws Exception{
        String indexUrl="http://www.itheima.com";

        /*HttpGet httpGet = new HttpGet(indexUrl);
        CloseableHttpClient httpClient = HttpClients.createDefault();

        httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36");

        CloseableHttpResponse resp = httpClient.execute(httpGet);

        HttpEntity entity = resp.getEntity();

        String s = EntityUtils.toString(entity);
        System.out.println(s);*/

        //3解析数据
        //3.1使用jsoup获取document对象
        Document document1 = Jsoup.connect(indexUrl).get();//只在测试中使用
        //System.out.println(document1);


        //3.2使用jsoup获取（方式2）      -------------------最常用的当时
        String html="<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "    <head>\n" +
                "        <meta charset=\"UTF-8\">\n" +
                "        <title>获得document对象</title>\n" +
                "    </head>\n" +
                "    <body>\n" +
                "\n" +
                "    </body>\n" +
                "</html>";
        Document document2 = Jsoup.parse(html);
        //System.out.println(document2);


        //3.3方式3
        //Document document3 = Jsoup.parse(new File(""), "UTF-8");


        //方式4 --------------不使用
        Document document4 = Jsoup.parseBodyFragment("<a href=\"http://www.baidu.com\">百度</a><span>柳淇</span>");
        System.out.println(document4);
    }
}
